<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_video extends Controller_Abstract {
	
	/**
	 * 视频
	 */
	function actionIndex()
	{
		$append=array("acct_id"=>$_SESSION['acct']['id']);
		$this->union_table = video::meta();
		$this->lists($append);
	}
	 
	/**
	 * 视频设置
	 */
	 
	function actionAdd() {
		if($this->_context->isGET()){
	    	if(!empty($this->_context->id)){
	    		$video = video::find('id=?',$this->_context->id)->getOne();
	    		$this->_view['video'] = $video;
	    	}
	    }
		if($this->_context->isPOST())
		{
			if(!empty($this->_context->id)){
				$video = video::find('id=?',$this->_context->id)->getOne();
			}else{
				$video = new video();
			}
			$video->teacher_id=intval($this->_context->teacher_id);
			$video->title=trim($this->_context->title);
			$video->class_id=intval($this->_context->class_id);
			$video->cat_id= educategory::getparentid(intval($this->_context->class_id));
			$video->virtual_sellnum = intval($this->_context->virtual_sellnum);
			$video->virtual_pintuannum = intval($this->_context->virtual_pintuannum);
			$video->tag_id = intval($this->_context->tag_id);
			$video->sort = intval($this->_context->sort);
			$video->status = intval($this->_context->status);
			$video->is_order = intval($this->_context->is_order);
			$video->sharefree_open = intval($this->_context->sharefree_open);
			$video->sharefree_days = intval($this->_context->sharefree_days);
			$video->page_view=intval($this->_context->page_view);
			$video->type=intval($this->_context->type);
			$video->style=intval($this->_context->style);
			$video->is_pay=intval($this->_context->is_pay);
			$video->is_password=intval($this->_context->is_password);
			$video->index_show=intval($this->_context->index_show);
			$video->inliveuser=intval($this->_context->inliveuser);;
			$video->present_price=$this->_context->present_price;
			$video->original_price=$this->_context->original_price;
			$video->money=$this->_context->money;
			if (!empty($this->_context->free_viplevelids)) {
				$video->free_viplevelids=implode(',',$this->_context->free_viplevelids);
			}else{
				$video->free_viplevelids="";
			}
			$video->opening_time=strtotime($this->_context->opening_time);
			$video->end_time=strtotime($this->_context->end_time);
			if($video->opening_time>$video->end_time){
				$this->error("开始时间不能大于结束时间");
			}
			$video->difficult_name=trim($this->_context->difficult_name);
			$video->pic_url=trim($this->_context->pic_url);
			$video->video_url=trim($this->_context->video_url);
			$video->live_url=trim($this->_context->live_url);
			$video->return_url=trim($this->_context->return_url);
			$video->content=trim($this->_context->content);
			$video->introduce=trim($this->_context->introduce);
			$video->password=trim($this->_context->password);
			$video->share_title=trim($this->_context->share_title);
			$video->share_ico=trim($this->_context->share_ico);
			$video->share_dec=trim($this->_context->share_dec);
			$video->tel=trim($this->_context->tel);
			$video->fans_content=trim($this->_context->fans_content);
			$host = Helper_Common::getRequestHost();
			$filename = Q::ini('app_config/ROOT_DIR').'/config/redis.yaml';
			$config = Helper_YAML::load($filename);
			$redis = new Redis();
			$redis->pconnect($config['host'],$config['port']);
	        if($redis->exists("edulive:$host:video:".$this->_context->id)){
	            $redis->del("edulive:$host:video:".$this->_context->id);
	        }
	        $video->save();
			if(!$_POST['id'])	{
	            Syslog::logs ( '添加视频,id :' . $video->id );
	        }else{
	        	
		        Syslog::logs ( '编辑视频,id :' . $video->id );
		    }
	        $this->_context->forwardUrl = url("video/index");
			$this->success('编辑成功');
		}
	}
	 
	/**
	 * 删除视频标签
	 */
	function actionDel() {
	     Syslog::logs("删除视频，ID：".print_r($this->_context->ids,true));
	     $this->union_table = video::meta();
	     $ids = $this->_context->ids;
	     $host = Helper_Common::getRequestHost();
	     	$filename = Q::ini('app_config/ROOT_DIR').'/config/redis.yaml';
			$config = Helper_YAML::load($filename);
			$redis = new Redis();
			$redis->pconnect($config['host'],$config['port']);
        for ($i=0; $i < count($ids); $i++) { 
        	if($redis->exists("edulive:$host:video:".$ids[$i])){
	            $redis->del("edulive:$host:video:".$ids[$i]);
	        }
        }
	    $this->delete(array('id'=>$ids));
	}

	function actioncomment(){
		$this->union_table = comment::meta();
		$this->lists();
	}

	/**
	 * 删除视频标签
	 */
	function actioncommentDel() {
	    Syslog::logs("删除留言，ID：".print_r($this->_context->ids,true));
	    $this->union_table = comment::meta();
	    $this->delete(array('id'=>$this->_context->ids));
	}

	/**
	 * 直播间在线
	 * @Author   xy
	 * @DateTime 2019-09-27T15:30:35+0800
	 * @return   [type]                   [description]
	 */
	function actionliveroom(){
		$video_id = $this->_context->video_id;
		global $root_dir;
		$host = $root_dir.'/cli/scripts/cli_socket.php';
		$filename = Q::ini('app_config/ROOT_DIR').'/config/redis.yaml';
		$config = Helper_YAML::load($filename);
		$redis = new Redis();
		$redis->pconnect($config['host'],$config['port']);
		$openids = array();
		$item = array();
		$room = $redis->get("edulive:$host:socket_pool:live:".$video_id);
		if($room){
			$room = json_decode($room,true);
			for ($i=0; $i < count($room); $i++) { 
				$fd = $redis->get("edulive:$host:socket_pool:pconnects:".$room[$i]);
				$fd = json_decode($fd,true);
				if($fd && !empty($fd['openid'])){
					array_push($openids, $fd['openid']);
				}
			}
		}
		
		$this->union_table = acctuser::meta();
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		$openidstr = "";
		//去重
		array_unique($openids);

		for ($i=0; $i < count($openids); $i++) { 
			$openidstr .= '"'.$openids[$i].'",';
		}

		if($openidstr){

			$openidstr = rtrim($openidstr,',');

			$sql = 'select * from acct_user where wechat_open_id in ('.$openidstr.')';
			$res = QDB::getConn()->execute($sql)->fetchAll();
		}else{

			$res = [];
		}

		$this->_view['item'] = $res;

	}

}


